package com.tengju.bff.interfaces.covercharge.controller.provider;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tengju.bff.interfaces.covercharge.constant.ContractFromEnum;
import com.tengju.bff.interfaces.covercharge.constant.CoverChargeErrorCode;
import com.tengju.bff.interfaces.covercharge.constant.IdentityTypeEnum;
import com.tengju.bff.interfaces.covercharge.constant.IsSigndEnum;
import com.tengju.bff.interfaces.covercharge.dto.SelfCertificationDto;
import com.tengju.bff.interfaces.covercharge.entity.po.ServiceSign;
import com.tengju.bff.interfaces.covercharge.entity.vo.LeTaxRegisteredAndReturnResult;
import com.tengju.bff.interfaces.covercharge.entity.vo.SelfCertificationQuery;
import com.tengju.bff.interfaces.covercharge.handler.CoverChargeException;
import com.tengju.bff.interfaces.covercharge.intf.INoticeMemberProvider;
import com.tengju.bff.interfaces.covercharge.service.IServiceLetaxRegisteredService;
import com.tengju.bff.interfaces.covercharge.service.IServiceSignService;
import com.tengju.bff.interfaces.covercharge.service.SelfCertificationService;
import com.tengju.bff.interfaces.shared.ApiResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.Objects;


@Slf4j
@Service("noticeMemberProvider")
public class NoticeMemberProvider implements INoticeMemberProvider {

    @Autowired
    private SelfCertificationService selfCertificationService;

    @Autowired
    private IServiceLetaxRegisteredService serviceLetaxRegisteredService;

    @Autowired
    private IServiceSignService serviceSignService;

    /**
     * 功能描述: <br>
     * 认证成功，通知会员
     *
     * @param selfCertificationDto
     */
    @Override
    public ApiResponse<?> signSuccessToNoticeMember(@RequestBody SelfCertificationDto selfCertificationDto) {
        log.info("认证成功，通知会员...{}", JSON.toJSONString(selfCertificationDto));
        Assert.notNull(selfCertificationDto.getIdCode(), "idCode不能为空");
        log.info("乐税注册成功，申请数字证书...{}", JSON.toJSONString(selfCertificationDto));
        Assert.notNull(selfCertificationDto.getIdCode(), "代理IdCode不能为空");
        Assert.notNull(selfCertificationDto.getPlatformCode(), "平台不能为空");
        Assert.notNull(selfCertificationDto.getCheckAt(), "认证时间不能为空");
        Assert.notNull(selfCertificationDto.getBeforeIdentity(), "认证之前身份不能为空");
        Assert.notNull(selfCertificationDto.getCurrentIdentity(), "当前是身份不能为空");

        //拼接请求参数
        SelfCertificationQuery selfCertificationQuery = new SelfCertificationQuery();

        //获取签约记录
        QueryWrapper<ServiceSign> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id_code", selfCertificationDto.getIdCode());
        queryWrapper.eq("platform_code", selfCertificationDto.getPlatformCode());
        ServiceSign serviceSign = serviceSignService.getOne(queryWrapper);
        if (Objects.isNull(serviceSign)) {
            return ApiResponse.newFailure(CoverChargeErrorCode.SYS_ERROR.getCode(), "没有签约信息");
        }
        if (!serviceSign.getIsSigned().equals(IsSigndEnum.YES.getCode())) {
            return ApiResponse.newFailure(CoverChargeErrorCode.SYS_ERROR.getCode(), "还未签约");
        }
//        ServiceSignRecordVo serviceSignRecord =serviceSignRecordService.queryFirst(serviceSign.getId(), SignFromEnum.SERVICE_PROVIDER.getCode());
//        if(Objects.isNull(serviceSignRecord)){
//            return JsonResult.failure(CoverChargeErrorCode.SYS_ERROR.getCode(), "没有签约记录信息");
//        }

        //拼接参数
        selfCertificationQuery.setIdCode(selfCertificationDto.getIdCode());
        selfCertificationQuery.setPlatformCode(selfCertificationDto.getPlatformCode());
        selfCertificationQuery.setCheckAt(selfCertificationDto.getCheckAt());
        selfCertificationQuery.setBeforeIdentity(selfCertificationDto.getBeforeIdentity());
        selfCertificationQuery.setCurrentIdentity(selfCertificationDto.getCurrentIdentity());
        selfCertificationQuery.setType(ContractFromEnum.LE_TAX_INDIVIDUAL.getFrom());

        //通知会员，身份认证成功
        //if(serviceSignRecord.getStatus().equals(SignRecordStatusEnum.VALID.getCode())){
        try {
            selfCertificationService.noticeStarService(selfCertificationQuery);
        } catch (Exception e) {
            log.error("signSuccessToNoticeMember error", e);
            return ApiResponse.newFailure(CoverChargeErrorCode.SYS_ERROR.getCode(), e.getMessage());
        }
        //}

        //申请数字证书开始

        //获取乐税返回信息
        LeTaxRegisteredAndReturnResult leTaxInfo = serviceLetaxRegisteredService.leTaxRegisteredAndReturnInfo(selfCertificationDto.getIdCode(), selfCertificationDto.getPlatformCode());
        log.info("乐税申请证书请求参数:{}", leTaxInfo);
        if (Objects.isNull(leTaxInfo)) {
            return ApiResponse.newFailure(CoverChargeErrorCode.SYS_ERROR.getCode(), "没有乐税注册信息");
        }
        selfCertificationQuery.setIdcardType(IdentityTypeEnum.TYPE_0.getType());
        selfCertificationQuery.setIdcardName(leTaxInfo.getIdcardName());
        selfCertificationQuery.setIdcardNo(leTaxInfo.getIdcardNo());
        selfCertificationQuery.setCompanyNo(leTaxInfo.getMerchantTaxNumber());
        selfCertificationQuery.setCompanyName(leTaxInfo.getMerchantName());
        selfCertificationQuery.setLegalMobile(leTaxInfo.getPhoneNumber());
        selfCertificationQuery.setType(ContractFromEnum.LE_TAX_INDIVIDUAL.getFrom());
        selfCertificationQuery.setIdCode(selfCertificationDto.getIdCode());
        selfCertificationQuery.setPlatformCode(selfCertificationDto.getPlatformCode());
        try {
            log.info("乐税申请证书请求参数:{}", selfCertificationQuery);
            selfCertificationService.registeredApplyCert(selfCertificationQuery);
            return ApiResponse.newSuccess();
        } catch (CoverChargeException ce) {
            log.error("乐税申请证书报错", ce);
            return ApiResponse.newFailure(CoverChargeErrorCode.SYS_ERROR.getCode(), ce.getMsg());
        } catch (Exception e) {
            log.error("自认证注册电子证书申请报错", e);
            return ApiResponse.newFailure(CoverChargeErrorCode.SYS_ERROR.getCode(), e.getMessage());
        }
    }
}
